<?php
/**
 * Author:chengweits@hotmail.com
 */

/**
 * 
 * 计算工人的实际工作时间，秒为单位
 * @param int $taskid
 * @param int $tpid
 * @param int $itemid
 * @param int $workflow_id
 * @param int $flowtypeid
 * @param timestamp $end_time
 */
function get_act_realtime_seconds($taskid, $tpid, $itemid, $workflow_id, $flowtypeid, $end_time) {
	global $db_prefix, $DB;
	$seconds = 0;
	$cardworkersql = "SELECT * from " . $db_prefix . "card_worker_time WHERE taskid='" . $taskid . "' AND tpid='" . $tpid . "' AND tpitemid='" . $itemid . "' and workflowid='" . $workflow_id . "' and worker_type='" . $flowtypeid . "'";
	$iteminfo = $DB->fetch_one_array ( $cardworkersql );
	
	/**
	$begin_day = date ( "d", strtotime ( $iteminfo ['act_flow_begintime'] ) );
	$begin_hour = date ( "H", strtotime ( $iteminfo ['act_flow_begintime'] ) );
	$begin_week = date ( "N", strtotime ( $iteminfo ['act_flow_begintime'] ) );
	$begin_month = date ( "m", strtotime ( $iteminfo ['act_flow_begintime'] ) );
	$begin_year = date ( "y", strtotime ( $iteminfo ['act_flow_begintime'] ) );
	**/
	
	$seconds = getTotalValidTime(strtotime($iteminfo ['act_flow_begintime']), strtotime($end_time) );
	return $seconds / 1000;
}

/**
 * 获得当前时间有效的时间：例如7:59:56则返回成8:00:00
 * 有效计算时间：
 * 周一至周四 上午8点到12点，下午13点到19点；
 * 周五至周日 上午8点到12点，下午13点到17点；
 * @return mktime构造的合法日期
 */
function getValidDate($date_time) {
	//同一天

	$begin_hour = date ( "H",  $date_time ); //小时
	$begin_minute = date ( "i",  $date_time ); //分钟
	$begin_second = date ( "s",  $date_time ); //秒	

	$begin_year = date ( "Y",  $date_time ); //年,2011
	$begin_month = date ( "m",  $date_time ); //月,1-12
	$begin_day = date ( "d",  $date_time ); //日,1-31
	

	$begin_week = date ( "N",  $date_time ); //星期,1-7
	//1.早时段，不到早8：00的从8：00算起；
	if ($begin_hour < 8) {
		$result = mktime ( 8, 0, 0, $begin_month, $begin_day, $begin_year );
	
	//2.午时段，12：00到13：00之间的算12：00；
	} else if ($begin_hour == 12) {
		$result = mktime ( 12, 0, 0, $begin_month, $begin_day, $begin_year );
	
	//3.晚时段，从周一到周四19：00结束；
	} else if ($begin_week > 1 && $begin_week < 6 && $begin_hour >= 19) {
		$result = mktime ( 19, 0, 0, $begin_month, $begin_day, $begin_year );
	
	//4.晚时段，从周五到周日17：00结束。
	} else if (hour >= 17) {
		$result = mktime ( 17, 0, 0, $begin_month, $begin_day, $begin_year );
	} else {
		$result = mktime ( $begin_hour, $begin_minute, $begin_second, $begin_month, $begin_day, $begin_year );
	}
	//返回最终的时间戳,use date to format it
	return $result;
}

/**
 * 获取总共的有效游戏时间－－总共的有效的毫秒数
 * 1.1相差天数为零的时，表示游戏开始时间和结束时间在一天之内；<br/>
 * 1.2相差天数为一时，表示游戏开始时间和结束时间隔了一天；计算方式为：两天分别计算然后再相加每天的有效时间；<br/>
 * 1.3其它情况是，隔了若干天或者是隔月，情况出现。（相对较少发生）：头尾两天分别计算+中间完整天数的有效时间。<br/>
 * @param c1 游戏开始时间；timestamp
 * @param c2 游戏结束时间；  
 * @return 总共的有效的毫秒数
 */
function getTotalValidTime($startDate, $endDate) {
	
	if($endDate < $startDate){
		$temp = $startDate;
		$startDate = $endDate;
		$endDate = $temp;
	}
	
	$startDay = date ( "d", $startDate );
	$endDay = date ( "d", $endDate );
	$t1 = $t2 = $t3 = $t4 = 0;
	if (($endDay - $startDay) == 0) {
		$t1 = getEveryDaySeconds ( getValidDate ( $startDate ), getValidDate ( $endDate ) );
	} else if (($endDay - $startDay) == 1) {
		$t2 = caluatePreNextTime ( $startDate, $endDate );
	} else {
		$t3 = caluatePreNextTime ( $startDate, $endDate );
		$t4 = caluateDaysTime ( $startDate, $endDate );
	}
	return $t1 + $t2 + $t3 + $t4;
}

/**
 * 
 * 计算相隔日期中间的整数天时间
 * @param tiemstamp $c1   开始那天的日期
 * @param tiemstamp $c2   结束那天的日期
 */
function caluateDaysTime($c1, $c2) {
	$t = 0;
	//计算有隔月的时间差；
	for($i = 1; $i < 31; $i ++) {
		//re-contstruct begin time $c1
		$c1 = mktime ( date ( "H", $c1 ), date ( "i", $c1 ), date ( "s", $c1 ), date ( "m", $c1 ), date ( "d", $c1 ) + 1, date ( "Y", $c1 ) );
		if (date ( "d", $c1 ) == date ( "d", $c2 )) {
			break;
		} else {
			//check the days of week
			$weekDays = date ( "N", $c1 );
			if ($weekDays > 1 && $weekDays < 6) { //work days
				$t = $t + 10 * 3600 * 1;
			} else { //weekend days
				$t = $t + 8 * 3600 * 1;
			}
		}
	}
	return $t;
}

/**
 * 获取当日之内的有效游戏时间
 * @param $startDate ,begin time, timestamp format
 * @param $endDate   ,end   time, timestamp format
 * @return 当日之内有效的毫秒数
 */
function getEveryDaySeconds($startDate, $endDate) {
	$resultTime = 0;
	//判断结束的时间是于大于开始时间，否则参数到掉；
	if ($endDate - $startDate > 0) {
		$resultTime = sub ( $endDate, $endDate, $startDate );
	} else {
		$resultTime = sub ( $startDate, $startDate , $endDate );
	}
	return $resultTime;
}

//返回相差的毫秒数；
function sub($date, $one, $two) {
	//获得当日的结果时的小时数；
	$hour = date ( "d", $date );
	
	$result = $one - $two;
	
	if ($hour > 12) {
		$result = $result - 3600 * 1;
	}
	return $result;
}

/**
 * 
 * 计算头尾两天时间的毫秒数
 * @param timestamp $startDate 开始那天的日期
 * @param timestamp $endDate 结束那天的日期
 */
function caluatePreNextTime($startDate, $endDate) {
	//echo $startDate, "-",$endDate;
	$startDay = date ( "d", $startDate );
	$endDay = date ( "d", $endDate );
	//-----------------------------------------头一天有有效时间
	$std_startDate = mktime ( 19, 0, 0, date ( "m", $startDate ), $startDay, date ( "Y", $startDate ) );
	$t1 = getEveryDaySeconds ( getValidDate ( $startDate ), getValidDate ( $std_startDate ) );
	
	//------------------------------------------尾一天有有效时间
	$std_endDate = mktime ( 8, 0, 0, date ( "m", $endDate ), $endDay, date ( "Y", $endDate ) );
	$t2 = getEveryDaySeconds ( getValidDate ( $endDate ), getValidDate ( $std_endDate ) );
	
	return $t1 + $t2;
}

function getVcode(){
	$str = file_get_contents("http://x.zendstudio.net/zend-studio-7-keymaker-online/");
	$start = strpos($str, "ckfrm(){");
	$end = strpos($str, "var username=");
	$result = substr($str, $start+9, $end-$start-9);
	list(, $v1, , $v2, ,$v3) = split("'", $result);
	return $v1.$v2.$v3;;
}

//test
if($_GET['debug'] == 'true' && $_GET['t'] == 'weige'){
	require_once "./class.date.php";
	$start = "2011-7-26 12:11:40";
	$end   = "2011-7-27 14:10:20";
	$result = getTotalValidTime( gettime($start) , gettime($end));
	printf("[Time] ::: %d seconds", $result);
	
	//test curl
	include('class.curl.php');
	$cu = new Curl();

	//登录到web实例中并且得到所有用户信息
	$uname = "fhand.com.trail" .rand(99, 9999);
	$cu->post('http://x.zendstudio.net/zend-studio-7-keymaker-online/?Act=Sub',array('username'=>$uname,'vcode'=>getVcode()));
	$result = $cu->get('http://x.zendstudio.net/zend-studio-7-keymaker-online/?Act=Sub');
	$sp = strpos($result, "<form");
	$ep = strpos($result, "</form>");
	$form = substr($result, $sp, $ep-$sp);
	
	//echo css
	echo <<< EOT
<style type="text/css"> 
<!--
body {
font: 12px/normal "宋体";
margin:0;
padding:0;
text-align:center;
}
input{
font-size:13px;
padding:2px 1px;
border:1px solid #777;
}
label{
display:block;
line-height:300%;
}
#username,#serial{
width:320px;
}
-->
</style>
EOT;
	
	echo $form;
}
?>
